package com.cloudsigns.factorymanager2.engineering.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.activity.FeedBackDtailForPatrolActivity;
import com.cloudsigns.factorymanager2.engineering.activity.FeedBackForPatrolActivity;
import com.cloudsigns.factorymanager2.engineering.adapter.MediaForPatrolAdapter;

import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.engineering.bean.PatrolOrderBean;

import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tony on 2018/5/27.
 */

public class FeedBackListForPatrolFragment extends Fragment implements ItemClickListener {
    private ScrollView scrollview;
    private LinearLayout ll_region;
    private LayoutInflater layoutInflater;

    private final String TAG = FeedBackListForPatrolFragment.class.getSimpleName();

    private PatrolOrderBean patrolOrderBean;
    Map<String, List<MediaBean>> mediaBeanMap = new HashMap<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_feedback_list_for_patrol, null);
        scrollview = view.findViewById(R.id.scrollview);
        ll_region = view.findViewById(R.id.ll_region);

        layoutInflater = getActivity().getLayoutInflater();

        return view;

    }

    public void initData(PatrolOrderBean patrolOrderBean) {
        if (null != patrolOrderBean) {
            mediaBeanMap.clear();

            this.patrolOrderBean = patrolOrderBean;


            List<MediaBean> mediaBeanList = patrolOrderBean.getMediaList();
            if (null != mediaBeanList) {
                for (MediaBean mediaBean : mediaBeanList) {
                    String areaName = mediaBean.getAreaName();
                    if (mediaBeanMap.containsKey(areaName)) {
                        mediaBeanMap.get(areaName).add(mediaBean);
                    } else {
                        List<MediaBean> tmpMediaBeanList = new ArrayList<>();
                        tmpMediaBeanList.add(mediaBean);
                        mediaBeanMap.put(areaName, tmpMediaBeanList);
                    }
                }
            }

            updateUi();
        }
    }

    private void updateUi() {
        if (null != mediaBeanMap && null != ll_region) {
            ll_region.removeAllViews();

            for (Map.Entry<String, List<MediaBean>> entry : mediaBeanMap.entrySet()) {
                View view = layoutInflater.inflate(R.layout.layout_point_location_content, null);
                TextView tv_area_name = view.findViewById(R.id.tv_area_name);
                tv_area_name.setText(entry.getKey());

                ListView lv_media = view.findViewById(R.id.lv_media);

                List<MediaBean> mediaBeanList = entry.getValue();
                MediaForPatrolAdapter adapter = new MediaForPatrolAdapter(getActivity(), TAG);
                adapter.setData(mediaBeanList);
                adapter.setItemClickListener(this);
                lv_media.setAdapter(adapter);


                ll_region.addView(view);


            }
        }

        scrollview.smoothScrollTo(0, 0);

    }

    @Override
    public void itemClick(View view, int position) {
        Intent mIntent;
        MediaBean mediaBean;
        switch (view.getId()) {
            case R.id.img_view:
                mIntent = new Intent(getActivity(), FeedBackDtailForPatrolActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                mIntent.putExtra(ConstantUtil.KEY_2, patrolOrderBean.getId());
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                break;
            case R.id.img_feedback:
                mediaBean = (MediaBean) view.getTag();
                mIntent = new Intent(getActivity(), FeedBackForPatrolActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, patrolOrderBean);
                mIntent.putExtra(ConstantUtil.KEY_2, mediaBean);
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                break;
        }
    }

}
